﻿using System;
using System.Web.UI;

using Carbon.Web.Forms.Controls;

namespace Carbon.Web.Forms.jQueryForms
{
    public abstract class jQueryFormsViewControlBase : ViewControlBase
    {
		#region Static Methods (1)
		internal static void RenderBoxes(HtmlTextWriter writer, bool errorHappened, bool showErrorText, String errorText, Action action)
        {
            string outerClass = Constants.OuterBoxClass;

            if (errorHappened)
            {
                outerClass = outerClass + " " + Constants.ValidationClass;
            }

            writer.AddAttribute(HtmlTextWriterAttribute.Class, outerClass);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            
            action();

            {
                if (!errorHappened || (errorHappened && !showErrorText))
                {
                    writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "none");
                }

                writer.AddAttribute(HtmlTextWriterAttribute.Class, Constants.ErrorClass);
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                {
                    if (errorText == null)
                        errorText = string.Empty;

                    // don't escape text, so error message can be html
                    writer.Write(errorText);
                }
                writer.RenderEndTag();
            }

            writer.RenderEndTag();
        }
		#endregion

		#region Fields (5)
		private bool m_ErrorDuringUpdateModel = false;
		private bool m_ShowErrorText = true;
		private bool m_StoreInvalidValueInViewModel = true;

		private String m_ErrorTextDuringUpdateModel = null;
		private String m_Text = default(String);
		#endregion

		#region Properties (7)
		public bool ErrorDuringUpdateModel
        {
            get
            {
                return m_ErrorDuringUpdateModel;
            }
            set
            {
                m_ErrorDuringUpdateModel = value;
            }
        }
		public String ErrorTextDuringUpdateModel
        {
            get
            {
                return m_ErrorTextDuringUpdateModel;
            }
            set
            {
                m_ErrorTextDuringUpdateModel = value;
            }
        }
		public bool IsTypeNullable
        {
            get
            {
                return InternalUtility.IsNullable(Binder.Property.PropertyType);
            }
        }
		public ScriptManager ScriptManager
        {
            get
            {
                ScriptManager sm = ScriptManager.GetCurrent(this.Page);
                if (sm == null) throw new InvalidOperationException("Script manager doesn't exist on page.");
                return sm;
            }
        }
		public bool ShowErrorText
        {
            get
            {
                return m_ShowErrorText;
            }
            set
            {
                m_ShowErrorText = value;
            }
        }
		public bool StoreInvalidValueInViewModel
        {
            get
            {
                return m_StoreInvalidValueInViewModel;
            }
            set
            {
                m_StoreInvalidValueInViewModel = value;
            }
        }
        /// <summary>
        /// Persists text value that is entered in textbox but isn't valid
        /// </summary>
		public String Text
        {
            get
            {
                return m_Text;
            }
            set
            {
                m_Text = value;
            }
        }
		#endregion

		#region Methods (1)

		public override void OnUpdateModel(object sender, UpdateModelEventArgs args)
        {
            try
            {
                if (!ErrorDuringUpdateModel)
                {
                    base.OnUpdateModel(sender, args);
                }
            }
            catch (ApplicationException ex)
            {
                this.IsValid = false;
                this.ErrorDuringUpdateModel = true;
                this.ErrorTextDuringUpdateModel = ex.Message;
            }
        }

		#endregion
    }
}
